package com.shenghui.enterprise.enterprise.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shenghui.enterprise.base.entity.BaseUser;
import com.shenghui.enterprise.base.entity.Page;
import com.shenghui.enterprise.base.entity.PageModel;
import com.shenghui.enterprise.enterprise.entity.EntEnterpriseInfo;
import com.shenghui.enterprise.enterprise.mapper.EntEnterpriseInfoMapper;
import com.shenghui.enterprise.enterprise.service.IEntEnterpriseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenghui.enterprise.utils.GeneralUtil;
import com.shenghui.enterprise.utils.UserUtil;
import com.shenghui.enterprise.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @Description: 企业基础信息表业务逻辑实现类
 * @Author tt
 * @Email: happyxiaohou@gmail.com
 * @Date: 2020-10-08
 * @Copyright (c) 2020-2022 云南昇晖科技有限公司
 */
@Service
@Transactional
public class EntEnterpriseInfoServiceImpl extends ServiceImpl<EntEnterpriseInfoMapper, EntEnterpriseInfo> implements IEntEnterpriseInfoService {

    @Autowired
    private EntEnterpriseInfoMapper entEnterpriseInfoMapper;
    @Autowired
    private UserUtil userUtil;

    @Override
    public Result addOrUpdate(EntEnterpriseInfo entEnterpriseInfo) {
        try {
            if (GeneralUtil.isNotNull(entEnterpriseInfo.getId())){
                entEnterpriseInfo.setUpdateDate(LocalDateTime.now());
                entEnterpriseInfo.setUpdateUserId(userUtil.getUserId());
                entEnterpriseInfoMapper.updateById(entEnterpriseInfo);
            } else {
                entEnterpriseInfo.setCreateDate(LocalDateTime.now());
                entEnterpriseInfo.setCreateUserId(userUtil.getUserId());
                entEnterpriseInfoMapper.insert(entEnterpriseInfo);
            }
            return Result.ok("操作成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("操作失败，请联系系统管理员!");
        }
    }

    @Override
    public PageModel findList(Map map) {
        Page<Map> mapPage = new Page<>(map);
        IPage iPage = entEnterpriseInfoMapper.selectPage(mapPage, new QueryWrapper<EntEnterpriseInfo>()
                .lambda()
                .like(GeneralUtil.isNotNull(map.get("entName")), EntEnterpriseInfo::getEntName, map.get("entName")));
        return new PageModel(iPage.getTotal(), iPage.getRecords());
    }
}
